//
//  HPTransferConfirmView.m
//  ZZTong
//
//  Created by handpay on 13-5-14.
//  Copyright (c) 2013年 handpay. All rights reserved.
//

#import "HPTransferConfirmView.h"
#import "DefineUtil.h"
#import "ZZTongDefine.h"
#import "CallbackData.h"
//#import "HPSwiperService.h" // mcm
#import <HandPaySwiperModule/HandPaySwiperModule.h>


@interface HPTransferConfirmView ()

@property (nonatomic, retain) UIImageView * contentView;
@property (nonatomic, retain) UIView * bgView;
@property (nonatomic, retain) UIImageView * bgImageView;
@property (nonatomic, retain) UIButton * cancelButton;
@property (nonatomic, retain) UIButton * okButton;

@property (nonatomic, retain) NSDictionary * dataDictionay;
@property (nonatomic, retain) CallbackData* callBackData;
@property (nonatomic, retain) CallbackData* cancelData;

@end

@implementation HPTransferConfirmView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initializeWithFrame:frame];
    }
    return self;
}
- (void)dealloc{
    [NOTIFICATION removeObserver:self];
    self.dataDictionay = nil;
    self.contentView = nil;
    self.bgView = nil;
    self.cancelButton = nil;
    self.okButton = nil;
    self.bgImageView = nil;
    self.callBackData = nil;
    [super dealloc];
}
- (void)awakeFromNib{
    [self initializeWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
}

- (int)createItemData:(NSString *)dataKey withTop:(int)top{
    
    if (!dataKey || [dataKey isEqualToString:@""]) {
        return top;
    }
    int y = top;
    int x = KLINESPACE;
    
    NSArray * dataArray = [self.dataDictionay objectForKey:dataKey];
    
    for (int i = 0; i < [dataArray count]; i++) {
        NSDictionary * dataDic = [dataArray objectAtIndex:i];
        
        CGRect labelRect = CGRectMake(x, y, KLABELWIDTH, KLINEHEIGHT);
        
        UILabel * label = [[UILabel alloc]initWithFrame:labelRect];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = [dataDic objectForKey:@"label"];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = kBkgColor;
        label.font = kNormalFont;
        [self.contentView addSubview:label];
        [label release];
        
        CGRect valueRect = CGRectMake(KLABELWIDTH + x, y, self.contentView.frame.size.width - KLABELWIDTH - 2 * KLINESPACE, KLINEHEIGHT);
        
        UILabel * labelValue = [[UILabel alloc]initWithFrame:valueRect];
        labelValue.textAlignment = NSTextAlignmentRight;
        labelValue.text = [dataDic objectForKey:@"value"];
        labelValue.textColor = kBkgColor;
        labelValue.font = kNormalFont;
        
        NSNumber * number = [dataDic objectForKey:@"features"];
        if (number && [number intValue] == 1) {
            labelValue.textColor = kGreenColor;
        }
        
        labelValue.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:labelValue];
        [labelValue release];
        
        y += KLINEHEIGHT + KLINESPACE;
    }
    return  y;
}
- (void)setContentData:(NSDictionary *)dataDic callback:(CallbackData*)callback{
    self.dataDictionay = dataDic;
    self.callBackData = callback;

//    for (UIView* view in [self.contentView subviews]) {
//        [view removeFromSuperview];
//    }
    int y = 10;
    int x = KLINESPACE;
    //上面数据
    y = [self createItemData:@"aboveData" withTop:y];
    
    //中间线
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(x, y, 295 - 2 * x, 1)];
    lineView.backgroundColor = kGrayColor;
    [self.contentView addSubview:lineView];
    [lineView release];
    
    y += 1 + KLINESPACE;
    
    //下面数据
    y = [self createItemData:@"belowData" withTop:y];
  
    int conntentViewH = y + 5;
    self.contentView.frame = CGRectMake(7, 10, 295, conntentViewH);
    
    int btnH = 45;
    self.bgView.frame = CGRectMake(5, (ScreenHeight - conntentViewH - 50) * 2 / 5 , 309, conntentViewH + 85);
    
    int btnW = (self.bgView.frame.size.width - 3 * KLINESPACE) / 2;
    
    int btnY = (self.bgView.frame.size.height - btnH - 15);
    
    [self.cancelButton setFrame:CGRectMake(KLINESPACE, btnY, btnW, btnH)];
    [self.okButton setFrame:CGRectMake(2 * KLINESPACE + btnW , btnY, btnW, btnH)];
    
    [self.bgImageView setFrame:self.bgView.frame];
    [self setHidden:NO];
    [self.superview bringSubviewToFront:self];
}

-(void)setCancelCallback:(CallbackData*)callback
{
    self.cancelData = callback;
}

#pragma mark - 私有方法

- (void) initializeWithFrame:(CGRect)frame{
    
//    self.frame = frame;
    
    self.backgroundColor = UIColorFromRGBA(0x000000, 0.75);
    
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    self.bgView = view;
    [view release];
    
    
    UIImage *innerImage = [UIImage imageNamed:@"bg_alert_inner.png"];
    UIImageView * innerView = [[UIImageView alloc] initWithImage:innerImage];
    self.contentView = innerView;
    [self.bgView addSubview:self.contentView];
    [innerView release];
    
    
    UIImage * buttonImage = [[UIImage imageNamed:@"bluebutton2.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    UIImage* hoverImage = [[UIImage imageNamed:@"bluebuttonhover2.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateHighlighted];
    [cancelButton addTarget:self action:@selector(buttonCancel) forControlEvents:UIControlEventTouchUpInside];
    self.cancelButton = cancelButton;
    [self.bgView addSubview:self.cancelButton];
    
    UIButton * okButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [okButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [okButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    [okButton setTitle:@"确定" forState:UIControlStateHighlighted];
    [okButton addTarget:self action:@selector(buttonOK) forControlEvents:UIControlEventTouchUpInside];
    self.okButton = okButton;
    [self.bgView addSubview:self.okButton];
    
    
    UIImage * blueImage = [UIImage imageNamed:@"bg_alert.png"];
    UIImageView * blueView = [[UIImageView alloc]initWithImage:blueImage];
    self.bgImageView = blueView;
    [self addSubview:self.bgImageView];
    [blueView release];
    
    [self addSubview:self.bgView];
    
    [NOTIFICATION addObserver:self selector:@selector(swiperStateChanged:) name:SwiperStateChangeNotificationKey object:nil];
    [NOTIFICATION addObserver:self selector:@selector(popToLoginScreen:) name:WillPopToLoginNotificationKey object:nil];
    
}
- (void)popToLoginScreen:(NSNotification*)notification{
    [self removeFromSuperview];
}

- (void)swiperStateChanged:(NSNotification*)notification{
    NSNumber* tempState = (NSNumber*)notification.object;
    SwiperState swiperState =(SwiperState) [tempState integerValue];
    switch (swiperState) {
        case SwiperStateNoDeviceDected:
            [self removeFromSuperview];
            break;
        default:
            break;
    }
}
- (void)buttonCancel{
    if (self.cancelData) {
        [self.cancelData.target performSelector:self.cancelData.callback withObject:nil];
    }
    [HPUIENG removeWaiting];
    [self removeFromSuperview];
}

- (void)buttonOK{
//    [self.superview sendSubviewToBack:self];
    if (self.callBackData) {
        [self.callBackData.target performSelector:self.callBackData.callback withObject:nil];
    }
    [self removeFromSuperview];
}


@end
